<?php
class Ecard extends CI_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->model("ecard_model",'ecard');
		
	}
	
    //新加
	public function add(){
		$this->load->helper('form');
		$this->load->library("form_validation");

		$this->form_validation->set_rules('title', 'title', 'required');
  		$this->form_validation->set_rules('info', 'info', 'required');
	

  		if($this->form_validation->run() === false){
			$this->load->view("templates/header");
  			$this->load->view("admin/ecard/add");
			$this->load->view("templates/footer");
  		}else{
			$this->load->model('upload_model');
			
			
			$data = array(
				'title' => $this->input->post('title'),
				'info' => $this->input->post('info')
			);
			
			  $config['upload_path'] = './data/upload/ecard/';
			  $config['allowed_types'] = 'gif|jpg|png';
			  $config['max_size'] = 2*1024;
			  $config['max_width']  = '1024';
			  $config['max_height']  = '768';
			
			
			$upmaster = $this->upload_model->upload('master',$config);
			$upthumb = $this->upload_model->upload('thumb',$config);
			
			if(!isset($upmaster['error'])){
				$data['master'] =  $config['upload_path'].$upmaster['file_name'];
			}else{
				$error[] = $upmaster['error'];
				$this->load->view("templates/header");
				$this->load->view("admin/ecard/add",array('error'=>$error));
				$this->load->view("templates/footer");
			}
		
			if(!isset($upthumb['error'])){
				$data['thumb'] =  $config['upload_path'].$upthumb['file_name'];
			}else{
				unlink($data['master']);
				$error[] = $upthumb['error'];
				$this->load->view("templates/header");
				$this->load->view("admin/ecard/add",array('error'=>$error));
				$this->load->view("templates/footer");
			}
			
			
			if(!isset($error)){
				//error, do something
				if($this->ecard->addEcard($data)){
					$this->lists();
				}
				
			}
  			
  		}
		
	}

	//更新
	public function edit(){

		$this->load->helper('form','url');
		
		if(isset($_POST['action'])){
			$action = $_POST['action'];
			
			//模板整体更新
			if($action == 'update'){
				$data = array(
					'title' => $this->input->post('title'),
					'info' => $this->input->post('info'),
					'master' => $this->input->post('oldmaster'),
					'thumb' => $this->input->post('oldthumb'),
					'status' => $this->input->post('status')
				);
				
				$this->load->model("upload_model");
				
			  $config['upload_path'] = './data/upload/ecard/';
			  $config['allowed_types'] = 'gif|jpg|png';
			  $config['max_size'] = 2*1024;
			  $config['max_width']  = '1024';
			  $config['max_height']  = '768';

				if($_FILES['master']['name'] != "" ){
						$master = $this->upload_model->upload('master',$config);
						
							if(!isset($master['error'])){
									$data['master'] = $config['upload_path'].$master['file_name'];
							}else{
								$error[] = $master['error'];
								showmessage($error,'','error');
							}
				}
				
				if( $_FILES['thumb']['name'] != "" ){
						$thumb = $this->upload_model->upload('thumb',$config);
						if(!isset($thumb['error'])){
							$data['thumb'] =  $config['upload_path'].$thumb['file_name'];
						}else{
							$error[] = $thumb['error'];// error 通知给全局的消息管理器
							showmessage($error,'error');
						}
						
				}
				
				

				$up = $this->ecard->editEcard($data,$this->input->post('id'));

				if($up){
					
					$this->lists();
					
				}else{
					$data = array(
						'ecards' => $this->ecard->ecardInfo(array('id'=>$this->input->post('id'))),
						'result' => '更新失败或内容未发生变化!'
					);
					$this->load->view("templates/header");
					$this->load->view('admin/ecard/edit',$data);
					$this->load->view("templates/footer");
				}
			}
			
			
			
		}else{
			
			$id = $this->uri->segment(3);//取得id
			$cards = $this->ecard->ecardInfo(array('id'=>$id));	

  			$data = array(
					'ecards' => $cards
  			);
			
			
			$this->load->view("templates/header");
			$this->load->view('admin/ecard/edit',$data);
			$this->load->view("templates/footer");
		}

	}
	
	//更新状态
	public function status(){
		//文章状态更新
			
				$data = array(
					'status' => $_POST['status']
				);
				
				$id = $this->input->post('id');
				
				$up = $this->ecard->up_status($id,$data);
				if($up){
					$json['error'] = 0;
					$json['content'] = $_POST['status'];
					echo json_encode($json);
					return ;
					
				}else{
					$json['error'] = 1;
					$json['content'] = $_POST['status'];
					echo json_encode($json);
					return ;
				}
			
			
	} 

	//删除
	public function delete(){
		//$id = $this->uri->segment(3);
		if($row = $this->ecard->deleteEcard($_POST['id'])){
			$json['error'] = 0;
			$json['content'] = '';
			echo json_encode($json);
			return ;
		}else{
			$json['error'] = 1;
			$json['content'] = '删除失败!';
			echo json_encode($json);
			return ;
		}
	
	}
	
	//全部列出
	public function lists(){
      	$this->load->library('pagination');

      	$config['base_url']=base_url("ecard/lists");

		$config['total_rows'] = $this->db->count_all('ecard');

		$config['pre_page'] = 10;

		$this->pagination->initialize($config);

		$data = array(
             "ecards" => $this->ecard->ecardList($config['pre_page'],$this->uri->segment(3)!==FALSE?$this->uri->segment(3):0)
		
        );
		//echo $this->db->last_query();
		$this->load->view("templates/header");
		$this->load->view("admin/ecard/list",$data);
		$this->load->view("templates/footer");
		
	}
	
	public function addAttr(){
		$this->load->helper('form','url');
		
		
		$data = array(
			'eid' => (int)$this->uri->segment(3)
		);
		
		
		$this->load->model('ecard_model');
		
		$data['attrs'] = $this->ecard_model->attrByEcard($data['eid']);
		
	
		$this->load->library("form_validation");
		$this->form_validation->set_rules('attr', 'attr', 'required');
  		$this->form_validation->set_rules('coordinate', 'coordinate', 'required');
		

  		if($this->form_validation->run() === false){
			$this->load->view('templates/header');
			$this->load->view('admin/ecard/addAttr',$data);
			$this->load->view('templates/footer');
		}else{
			$data = array(
				'eid' => $this->input->post('eid'),
				'attr' => $this->input->post('attr'),
				'type' => $this->input->post('type'),
				'coordinate' => $this->input->post('coordinate'),
				'order' => $this->input->post('order'),
				'size' => $this->input->post('size'),
				'tips' => $this->input->post('tips')
			);
			if($this->ecard->addAttr($data)){
				$this->lists();
			}
		}
		
	}
	
	public function addAttrAjax(){
		$this->load->helper('form','url');
		$data = array(
			'eid' => $this->uri->segment(3),
			'attr' => $this->input->post('attr'),
			'type' => $this->input->post('type'),
			'coordinate' => $this->input->post('coordinate'),
			'order' => $this->input->post('order'),
			'size' => $this->input->post('size'),
			'tips' => $this->input->post('tips')
		);
			
		 $result['success'] = $this->ecard->addAttr($data);
		 echo json_encode($result);
		 exit;

	}
	
	
	public function editAttr(){
		$data = array(
			'attr' => $this->input->post('attr'),
			'coordinate' => $this->input->post('coordinate'),
			'type' => $this->input->post('type'),
			'order' => $this->input->post('order'),
			'size' => $this->input->post('size'),
			'tips' => $this->input->post('tips')
		);
		
		$where = array(
			'id' => $this->input->post('id')
		);
		
		 $result['success'] = $this->ecard->editeAttr($data,$where);
		 echo json_encode($result);
		 exit;
		
	}
	
	public function  deleteAttr(){
		$this->ecard->deleteAttr($this->input->post('id'));
	}



}